<!DOCTYPE html>
<html class="loading" lang="en" data-textdirection="ltr">
  <!-- BEGIN: Head-->
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <meta name="description" content="Materialize is a Material Design Admin Template,It's modern, responsive and based on Material Design by Google.">
    <meta name="keywords" content="materialize, admin template, dashboard template, flat admin template, responsive admin template, eCommerce dashboard, analytic dashboard">
    <meta name="author" content="ThemeSelect">
    <title>生态圈系统-企业列表</title>
    <link rel="icon" th:href="@{/images/favicon.ico}" th:mce_href="@{/images/favicon.ico}" type="image/x-icon">
    <link rel="shortcut icon" th:href="@{/images/favicon.ico}" th:mce_href="@{/images/favicon.ico}" type="image/x-icon">
    <link rel="apple-touch-icon" th:href="@{/images/favicon.ico}" th:mce_href="@{/images/favicon.ico}" type="image/x-icon">

    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link href="css/bootstrap.min.css" th:href="@{/css/bootstrap.css}" type="text/css" rel="stylesheet"/>
    <!-- BEGIN: VENDOR CSS-->
    <link rel="stylesheet" type="text/css" href="../../../app-assets/vendors/vendors.min.css" th:href="@{/app-assets/vendors/vendors.min.css}">
    <link rel="stylesheet" type="text/css" href="../../../app-assets/vendors/flag-icon/css/flag-icon.min.css" th:href="@{/app-assets/vendors/flag-icon/css/flag-icon.min.css}">
    <link rel="stylesheet" type="text/css" href="../../../app-assets/vendors/data-tables/css/jquery.dataTables.min.css" th:href="@{/app-assets/vendors/data-tables/css/jquery.dataTables.min.css}">
    <link rel="stylesheet" type="text/css" href="../../../app-assets/vendors/data-tables/extensions/responsive/css/responsive.dataTables.min.css" th:href="@{/app-assets/vendors/data-tables/extensions/responsive/css/responsive.dataTables.min.css}">
    <link rel="stylesheet" type="text/css" href="../../../app-assets/vendors/data-tables/css/select.dataTables.min.css" th:href="@{/app-assets/vendors/data-tables/css/select.dataTables.min.css}">
    <!-- END: VENDOR CSS-->
    <!-- BEGIN: Page Level CSS-->
    <link rel="stylesheet" type="text/css" href="../../../app-assets/css/themes/vertical-gradient-menu-template/materialize.css" th:href="@{/app-assets/css/themes/vertical-gradient-menu-template/materialize.css}">
    <link rel="stylesheet" type="text/css" href="../../../app-assets/css/themes/vertical-gradient-menu-template/style.css" th:href="@{/app-assets/css/themes/vertical-gradient-menu-template/style.css}">
    <link rel="stylesheet" type="text/css" href="../../../app-assets/css/pages/data-tables.css" th:href="@{/app-assets/css/pages/data-tables.css}">
    <!-- END: Page Level CSS-->
    <!-- BEGIN: Custom CSS-->
    <link href="plugins/bootstrap-datepicker/css/bootstrap-datepicker.css" th:href="@{/plugins/bootstrap-datepicker/css/bootstrap-datepicker.css}" type="text/css" rel="stylesheet"/>
	  <link href="plugins/bootstrap-sweetalert/sweetalert.css" th:href="@{/plugins/bootstrap-sweetalert/sweetalert.css}" type="text/css" rel="stylesheet"/>
	  <link rel="stylesheet" type="text/css" href="../../../app-assets/css/custom/custom.css" th:href="@{/app-assets/css/custom/custom.css}">
    <!-- END: Custom CSS-->
  </head>
  <!-- END: Head-->
  <body class="vertical-layout page-header-light vertical-menu-collapsible vertical-gradient-menu 2-columns" data-open="click" data-menu="vertical-gradient-menu" data-col="2-columns">

    <!-- BEGIN: Header-->
    <div th:replace="common/header :: header"></div>
    <!-- END: Header-->

    <div th:replace="common/aside_admin :: aside"></div>

    <!-- BEGIN: Page Main-->
    <div id="main">
      <div class="row">
        <div class="pt-3 pb-1" id="breadcrumbs-wrapper">
          <!-- Search for small screen-->
          <div class="container">
            <div class="row">
              <div class="col s12 m6 l6">
                <h5 class="breadcrumbs-title mt-0 mb-0" id="page-title" active-menu="company-menu">企业列表</h5>
              </div>
              <div class="col s12 m6 l6 right-align-md hide">
                <ol class="breadcrumbs mb-0">
                  <li class="breadcrumb-item"><a href="index.html">Home</a></li>
                  <li class="breadcrumb-item"><a href="#">Pages</a></li>
                  <li class="breadcrumb-item active">Blank Page</li>
                </ol>
              </div>
            </div>
          </div>
        </div>
        <div class="col s12">
          <div class="container">
            <div class="section section-data-tables">
              <div class="row">
                <div class="col s12 m12 l12">
                  <div id="button-trigger" class="card card card-default scrollspy">
                    <div class="card-content">
                      <div class="row">
                        <div class="col s12">
                          <form id="search">
                            <div class="input-field col s2">
							                <input type="text" name="name" id="companyName" th:value="${search.name}">
							                <label for="companyName">企业名称</label>
							              </div>
							              <div class="input-field col s2">
							                <select name="categoryId" id="companyType">
							                  <option value="" selected>全部</option>
							                  <option th:each="c : ${categories}" th:value="${c.id}"  th:text="${c.name}" th:selected="${c.id == search.category?.id}">企业1</option>
							                </select>
							                <label for="companyType">企业类型</label>
							              </div>
							              <div class="input-field col s2">
							                <input type="text" name="contacts" id="contact" th:value="${search.contacts}">
							                <label for="contact">联系人</label>
							              </div>
							              <div class="input-field col s2">
							                <input type="text" name="phone" id="contactWay" th:value="${search.phone}">
							                <label for="contactWay">联系电话</label>
							              </div>
							              <div class="input-field col s1">
							                <a th:href="'javascript:search();'" class="waves-effect waves-light btn-floating search-btn"><i class="material-icons right">search</i></a>
							              </div>
							              <div class="col s2">
							                <div style="position: relative; height: 70px;">
							                </div>
							              </div>
							              <div class="input-field col s1">
							                <a class="waves-effect waves-light btn-floating btn-small blue right modal-trigger" href="#modal2"><i class="large material-icons">add</i></a>
							              </div>
                          </form>
                        </div>
                        <div class="col s12">
                          <table id="data-table-simple" class="display nowrap">
                            <thead>
                              <tr>
                                <th>企业ID</th>
							                  <th>企业名称</th>
							                  <th>企业类型</th>
							                  <th>地址</th>
							                  <th>联系人</th>
							                  <th>联系电话</th>
							                  <th>备注</th>
							                  <th>操作</th>
                              </tr>
                            </thead>
                            <tbody>
                            </tbody>
                            <tfoot class="hide">
                              <tr>
                                <th>企业ID</th>
                                <th>企业名称</th>
                                <th>企业类型</th>
                                <th>地址</th>
                                <th>联系人</th>
                                <th>联系电话</th>
                                <th>备注</th>
                                <th>操作</th>
                              </tr>
                            </tfoot>
                          </table>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- END: Page Main-->

    <!-- BEGIN: Footer-->

    <div th:replace="common/footer :: footer"></div>

    <!-- END: Footer-->

    <div id="modal1" class="modal modal-fixed-footer">
	    <div class="modal-header">
	      <h6>编辑企业</h6>
	      <a class="btn-flat modal-close modal-header-close"><i class="material-icons">close</i></a>
	    </div>
	    <div class="modal-content" style="border: unset; border-radius: 0;">
	      <div class="row">
	        <form class="col s12">
	          <input name="id" type="hidden">
	          <div class="row">
	            <div class="col s12 input-field">
	              <input type="text" name="name" id="name1" class="validate" required maxlength="50">
	              <label for="name1"><font color="red">*</font>企业名称</label>
	              <span class="helper-text" data-error="必填项" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field">
	              <select name="categoryId">
	                <option value="" selected>请选择</option>
	                <option th:each="c : ${categories}" th:value="${c.id}"  th:text="${c.name}">企业1</option>
	              </select>
	              <label for="categoryId1">企业类型</label>
	              <span class="helper-text" data-error="error" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field">
	              <input type="text" name="address" id="address1" class="validate" required maxlength="400">
	              <label for="address1"><font color="red">*</font>地址</label>
	              <span class="helper-text" data-error="必填项" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field" style="margin-top: 1.5rem;">
	              <input type="text" name="contacts" id="contacts1" class="validate" required maxlength="20">
	              <label for="contacts1"><font color="red">*</font>联系人</label>
	              <span class="helper-text" data-error="必填项" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field" style="margin-top: 1.5rem;">
	              <input type="tel" name="phone" id="phone1" class="validate" required maxlength="11">
	              <label for="phone1"><font color="red">*</font>联系电话</label>
	              <span class="helper-text" data-error="必填项，必须为手机号格式" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field" style="margin-top: 1.5rem;">
	              <input type="tel" name="remark" id="remark1" class="validate" maxlength="400">
	              <label for="remark1">备注</label>
	              <span class="helper-text" data-error="wrong" data-success="right"></span>
	            </div>
	          </div>
	          <input type="reset" style="display:none;" />
	        </form>
	      </div>
	    </div>
	    <div class="modal-footer">
	      <a href="#!" class="modal-close waves-effect waves-light btn-flat" style="margin-right: 10px;">取消</a>
	      <button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action" style="margin-right: 10px;" onclick="editSubmit()">确认</button>
	    </div>
	  </div>

	  <div id="modal2" class="modal modal-fixed-footer">
	    <div class="modal-header">
	      <h6>新增信托客户</h6>
	      <a class="btn-flat modal-close modal-header-close"><i class="material-icons">close</i></a>
	    </div>
	    <div class="modal-content" style="border: unset; border-radius: 0;">
	      <div class="row">
	        <form class="col s12">
	          <div class="row">
	            <div class="col s12 input-field">
	              <input type="text" name="name" id="name" class="validate" required maxlength="50">
	              <label for="name"><font color="red">*</font>企业名称</label>
	              <span class="helper-text" data-error="必填项" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field">
	              <select name="categoryId">
	                <option value="" selected>请选择</option>
	                <option th:each="c : ${categories}" th:value="${c.id}"  th:text="${c.name}">企业1</option>
	              </select>
	              <label for="categoryId">企业类型</label>
	              <span class="helper-text" data-error="error" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field">
	              <input type="text" name="address" id="address" class="validate" required maxlength="400">
	              <label for="address"><font color="red">*</font>地址</label>
	              <span class="helper-text" data-error="必填项" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field" style="margin-top: 1.5rem;">
	              <input type="text" name="contacts" id="contacts" class="validate" required maxlength="10">
	              <label for="contacts"><font color="red">*</font>联系人</label>
	              <span class="helper-text" data-error="必填项" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field" style="margin-top: 1.5rem;">
	              <input type="tel" name="phone" id="phone" class="validate" required maxlength="11">
	              <label for="phone"><font color="red">*</font>联系电话</label>
	              <span class="helper-text" data-error="必填项，必须为手机号格式" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field" style="margin-top: 1.5rem;">
	              <input type="tel" name="remark" id="remark" class="validate" maxlength="400">
	              <label for="remark">备注</label>
	              <span class="helper-text" data-error="wrong" data-success="right"></span>
	            </div>
	          </div>
	          <input type="reset" style="display:none;" />
	        </form>
	      </div>
	    </div>
	    <div class="modal-footer">
	      <a href="#!" class="modal-close waves-effect waves-light btn-flat" style="margin-right: 10px;">取消</a>
	      <button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action" style="margin-right: 10px;" onclick="addSubmit()">确认</button>
	    </div>
	  </div>

	  <div id="modal4" class="modal modal-fixed-footer">
	    <div class="modal-header">
	      <h6>新增企业</h6>
	      <a class="btn-flat modal-close modal-header-close"><i class="material-icons">close</i></a>
	    </div>
	    <div class="modal-content" style="border: unset; border-radius: 0;">
	      <div class="row">
	        <form class="col s12">
	          <div class="row">
	            <div class="col s12 input-field" style="margin-top: 1.5rem;">
	              <input type="tel" name="phone" id="phone2" required disabled>
	              <label for="phone2"><font color="red">*</font>联系电话</label>
	              <span class="helper-text" data-error="wrong" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field">
	              <input type="text" name="code" id="code" class="validate" required>
	              <label for="code"><font color="red">*</font>验证码</label>
	              <span class="helper-text" data-error="wrong" data-success="right"></span>
	            </div>
	            <div class="input-field">
	              <a class="btn waves-effect waves-light light-blue darken-1 white-text" style="margin-right: 10px;" href="javascript:sendSms();">发送验证码</a>
	            </div>
	          </div>
	          <input type="reset" style="display:none;" />
	        </form>
	      </div>
	    </div>
	    <div class="modal-footer">
	      <a href="javascript:backToOpenModal();" class="modal-close waves-effect waves-light btn-flat" style="margin-right: 10px;">上一步</a>
	      <button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action" style="margin-right: 10px;" onclick="addSubmit()">确定</button>
	    </div>
	  </div>

	  <div id="modal5" class="modal modal-fixed-footer">
	    <div class="modal-header">
	      <h6>重置密码</h6>
	      <a class="btn-flat modal-close modal-header-close"><i class="material-icons">close</i></a>
	    </div>
	    <div class="modal-content" style="border: unset; border-radius: 0;">
	      <div class="row">
	        <form class="col s12">
	          <input name="id" type="hidden">
	          <div class="row">
	            <div class="col s12 input-field" style="margin-top: 1.5rem;">
	              <input type="tel" name="phone" id="phone1" required disabled>
	              <label for="phone">短信发送至手机</label>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s8 input-field">
	              <input type="text" name="code" id="code1" class="validate" required>
	              <label for="code1"><font color="red">*</font>验证码</label>
	              <span class="helper-text" data-error="必填项" data-success="right"></span>
	            </div>
	            <div class="col s3 input-field">
	              <a class="btn waves-effect waves-light light-blue darken-1 white-text" id="sendSmsBtn" style="margin-right: 10px;" href="javascript:sendSms();">发送验证码</a>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field">
	              <input type="password" name="newPassword" id="newPassword1" class="validate" required value="000000">
	              <label for="newPassword1"><font color="red">*</font>新密码（默认为000000）</label>
	              <span class="helper-text" data-error="必填项" data-success="right"></span>
	            </div>
	          </div>
	          <div class="row">
	            <div class="col s12 input-field">
	              <input type="password" name="confirmPassword" id="confirmPassword1" class="validate" required value="000000">
	              <label for="confirmPassword1"><font color="red">*</font>确认密码</label>
	              <span class="helper-text" data-error="必填项，必须与上面填写一致" data-success="right"></span>
	            </div>
	          </div>
	          <input type="reset" style="display:none;" />
	        </form>
	      </div>
	    </div>
	    <div class="modal-footer">
	      <a href="#!" class="modal-close waves-effect waves-light btn-flat" style="margin-right: 10px;">取消</a>
	      <button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action" style="margin-right: 10px;" onclick="resetPwdSubmit()">确定</button>
	    </div>
	  </div>

	  <div th:replace="common/update_pwd :: #modal3"></div>

    <!-- BEGIN VENDOR JS-->
    <script src="../../../app-assets/js/vendors.min.js" th:src="@{/app-assets/js/vendors.min.js}" type="text/javascript"></script>
    <!-- BEGIN VENDOR JS-->
    <!-- BEGIN PAGE VENDOR JS-->
    <script src="../../../app-assets/vendors/data-tables/js/jquery.dataTables.min.js" th:src="@{/app-assets/vendors/data-tables/js/jquery.dataTables.min.js}" type="text/javascript"></script>
    <script src="../../../app-assets/vendors/data-tables/extensions/responsive/js/dataTables.responsive.min.js" th:src="@{/app-assets/vendors/data-tables/extensions/responsive/js/dataTables.responsive.min.js}" type="text/javascript"></script>
    <script src="../../../app-assets/vendors/data-tables/js/dataTables.select.min.js" th:src="@{/app-assets/vendors/data-tables/js/dataTables.select.min.js}" type="text/javascript"></script>
    <!-- END PAGE VENDOR JS-->
    <!-- BEGIN THEME  JS-->
    <script src="../../../app-assets/js/plugins.js" th:src="@{/app-assets/js/plugins.js}" type="text/javascript"></script>
    <script src="../../../app-assets/js/custom/custom-script.js" th:src="@{/app-assets/js/custom/custom-script.js}" type="text/javascript"></script>
    <!-- END THEME  JS-->
    <!-- BEGIN PAGE LEVEL JS-->
    <script src="../../../app-assets/js/scripts/data-tables.js" th:src="@{/app-assets/js/scripts/data-tables.js}" type="text/javascript"></script>
    <!-- END PAGE LEVEL JS-->
    <script src="plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js" th:src="@{/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js}"></script>
    <script src="plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js" th:src="@{/plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js}" charset="UTF-8"></script>
    <script src="plugins/bootstrap-sweetalert/sweetalert.min.js" th:src="@{/plugins/bootstrap-sweetalert/sweetalert.min.js}"></script>
    <script src="js/common.js" th:src="@{/js/common.js}"></script>
    <script th:inline="javascript">
	    $(document).ready(function() {

	    	var dataTableSimple = $('#data-table-simple').DataTable({
		    	    "responsive": false,
		    	    "language" : language,
		    	    "ordering": false,
		          "serverSide": true,  //启用服务器端分页
		          "pagingType": "simple_numbers",
	            "ajax": function (data, callback, settings) {
                //封装请求参数
                var param = {};
                param.size = data.limit;//页面显示记录条数，在页面显示每页显示多少项的时候
                //param.start = data.start;//开始的记录序号
                param.page = data.page;//当前页码
                console.log(data);
                //console.log(param);
                //ajax请求数据
                $.ajax({
                   url: context_path + "/api/v1/companies/service/query?page=" + (data.start ? (data.start/data.length) : 0),
                   contentType: "application/json;charset=utf-8",
                   type: "POST",
                   headers: {
                     Accept: "application/json"
                   },
                   dataType: "json",
                   data: '{"name": "' + $("#search input[name='name']").val() + '",'
                	   + ($('#search select[name="categoryId"]').val() ? ' "category": {"id": "' + $('#search select[name="categoryId"]').val()  + '"},' : '')
                	   + ' "contacts": "' + $('#search input[name="contacts"]').val()  + '",'
                	   + ' "phone": "' + $('#search input[name="phone"]').val()  + '",'
                	   + ' "page": ' + (data.start ? (data.start/data.length) : 0) + ','
                	   + ' "size": 10'
                	   + '}',
                   success: function (result) {
                     //封装返回数据
                     var returnData = {};
                     //returnData.draw = data.draw;//这里直接自行返回了draw计数器,应该由后台返回
                     returnData.recordsTotal = result.totalElements;//返回数据全部记录
                     returnData.recordsFiltered = result.totalElements;//后台不实现过滤功能，每次查询均视作全部结果
                     returnData.data = result.content;//返回的数据列表
                     //console.log(returnData);
                     //调用DataTables提供的callback方法，代表数据已封装完成并传回DataTables进行渲染
                     //此时的数据需确保正确无误，异常判断应在执行此回调前自行处理完毕
                     callback(returnData);
                   }
                });
	            },
	            "columns": [
                { "data": "id" },
                { "data": "name" },
                {
                	"data": "category.name",
                	"defaultContent": ""
               	},
                { "data": "address" },
                { "data": "contacts" },
                { "data": "phone" },
                { "data": "remark" },
                { "data": null }
              ],
              "columnDefs": [
                {
                  targets: 7,
                  render: function (data, type, row, meta) {
                    return '<a href="javascript:openEditModal(' + data.id + ');" title="编辑"><i class="material-icons">edit</i></a>'
                         + '<a href="javascript:deleteSubmit(' + data.id + ');" title="删除"><i class="material-icons red-text text-lighten-2">delete</i></a>'
                         + '<a href="javascript:openResetPwdModal(' + data.id + ', ' + data.phone + ');" title="重置密码"><i class="material-icons red-text text-lighten-2">sync</i></a>';
                  }
                },
                { "orderable": false, "targets": 7 }
              ]

	    	    });

	    });

	    function search() {
        window.location.href = "companies?" + formParamsSerialize($("#search").find('input, select'));
      }

      function addSubmit() {
        var input_elems = $('#modal2 .modal-content input, #modal2 .modal-content textarea');
        input_elems.each(function(){
          M.validate_field($(this));
        });
        if(!checkPhone($('#modal2 input[name="phone"]').val())) {
          $('#modal2 input[name="phone"]').addClass('invalid');
        } else {
          $('#modal2 input[name="phone"]').removeClass('invalid');

        }
        var invalid_input_elems = $('#modal2 .modal-content input.invalid, #modal2 .modal-content textarea.invalid');
        if(invalid_input_elems.length > 0) {
          return false;
        }
        $.ajax({
          url: context_path + "/api/v1/companies/",
          data: '{"name": "' + $('#modal2 input[name="name"]').val()
              + ($('#modal2 select[name="categoryId"]').val() ? '", "category": {"id": ' + $('#modal2 select[name="categoryId"]').val() + '}' : '"')
              + ', "address": "' + $('#modal2 input[name="address"]').val()
              + '", "contacts": "' + $('#modal2 input[name="contacts"]').val()
              + '", "phone": "' + $('#modal2 input[name="phone"]').val()
              + '", "remark": "' + $('#modal2 input[name="remark"]').val()+ '"}',
          contentType: "application/json;charset=utf-8",
          type: "POST",
          headers: {
            Accept: "application/json"
          },
          success: function(data, textStatus) {
            console.log(data);
            swal({
              title: "",
              text: "保存成功!",
              icon: "success",
              timer: 2000,
            }).then(function(value) {
              $("#modal2 input[type=reset]").trigger("click");
              $('#modal2').modal('close');
              window.location.reload();
            });
          },
          error: function (data, textStatus, errorThrown) {
            //console.log(data);
            swal({
              title: "保存失败!",
              text: data.responseJSON.message,
              icon: "error",
              //timer: 2000,
            });
            M.updateTextFields();
          }
        });

      }

      function openSendCodeModal() {
        var input_elems = $('#modal2 .modal-content input, #modal2 .modal-content textarea');
        input_elems.each(function(){
          M.validate_field($(this));
        });
        if(!checkPhone($('#modal2 input[name="phone"]').val())) {
          $('#modal2 input[name="phone"]').addClass('invalid');
        } else {
          $('#modal2 input[name="phone"]').removeClass('invalid');

        }
        var invalid_input_elems = $('#modal2 .modal-content input.invalid, #modal2 .modal-content textarea.invalid');
        if(invalid_input_elems.length > 0) {
          return false;
        }
        $('#modal4 input[name="phone"]').val($('#modal2 input[name="phone"]').val());
        M.updateTextFields();
        $('#modal2').modal('close');
        $('#modal4').modal('open');
      }

      function backToOpenModal() {
        $('#modal2').modal('open');
        $('#modal4').modal('close');
      }

      function openEditModal(companyId) {
        $.ajax({
          url: context_path + "/api/v1/companies/" + companyId,
          contentType: "application/json;charset=utf-8",
          type: "GET",
          headers: {
            Accept: "application/json"
          },
          success: function(data, textStatus) {
            $("#modal1 input[type=reset]").trigger("click");
            $('#modal1 input[name="id"]').val(data.id);
            $('#modal1 input[name="name"]').val(data.name);
            $('#modal1 select[name="categoryId"] option').each(function(){
              if(data.category != null && $(this).attr('value') == data.category.id) {
                $(this).attr('selected', 'selected');
              } else {
                $(this).removeAttr('selected');
              }
            });
            $('#modal1 select[name="categoryId"]').formSelect();
            //$('#modal1 input[name="category"]').val(data.category);
            $('#modal1 input[name="address"]').val(data.address);
            $('#modal1 input[name="contacts"]').val(data.contacts);
            $('#modal1 input[name="phone"]').val(data.phone);
            $('#modal1 input[name="remark"]').val(data.remark);
            M.updateTextFields();
            $('#modal1').modal('open');
          }
        });
      }

      function editSubmit() {
        var input_elems = $('#modal1 .modal-content input, #modal1 .modal-content textarea');
        input_elems.each(function(){
          M.validate_field($(this));
        });
        if(!checkPhone($('#modal1 input[name="phone"]').val())) {
          $('#modal1 input[name="phone"]').addClass('invalid');
        } else {
          $('#modal1 input[name="phone"]').removeClass('invalid');

        }
        var invalid_input_elems = $('#modal1 .modal-content input.invalid, #modal1 .modal-content textarea.invalid');
        if(invalid_input_elems.length > 0) {
          return false;
        }
        $.ajax({
          url: context_path + "/api/v1/companies/" + $('#modal1 input[name="id"]').val(),
          data: '{"name": "' + $('#modal1 input[name="name"]').val()
              + ($('#modal1 select[name="categoryId"]').val() ? '", "category": {"id": ' + $('#modal1 select[name="categoryId"]').val() + '}' : '"')
              + ', "address": "' + $('#modal1 input[name="address"]').val()
              + '", "contacts": "' + $('#modal1 input[name="contacts"]').val()
              + '", "phone": "' + $('#modal1 input[name="phone"]').val()
              + '", "remark": "' + $('#modal1 input[name="remark"]').val()
              + '", "id": ' + $('#modal1 input[name="id"]').val() + '}',
          contentType: "application/json;charset=utf-8",
          type: "PUT",
          headers: {
            Accept: "application/json"
          },
          success: function(data, textStatus) {
            //console.log(data);
            swal({
              title: "",
              text: "修改成功!",
              icon: "success",
              timer: 2000,
            }).then(function(value) {
              $("#modal1 input[type=reset]").trigger("click");
              $('#modal1').modal('close');
              window.location.reload();
            });
            //$('#modal2').modal();
          },
          error: function (data, textStatus, errorThrown) {
            console.log(data);
            swal({
              title: "修改失败!",
              text: (data.responseJSON.message == '' ? "请联系管理员!" : data.responseJSON.message),
              icon: "error",
              //timer: 2000,
            });
            M.updateTextFields();
          }
        });
      }

      function deleteSubmit(companyId) {
        swal({
          title: "确认删除该企业吗?",
          text: "一旦删除，将无法找回或查看该企业!",
          icon: "warning",
          buttons: {
            cancel: {
              text: "取消",
              value: null,
              visible: true,
              className: "waves-effect waves-light btn-flat",
              closeModal: true,
            },
            confirm: {
              text: "确认",
              className: "waves-effect waves-light"
            }
          },
          dangerMode: true,
        })
        .then(function(willDelete) {
          if (willDelete) {
            //console.log(willDelete);
            $.ajax({
              url: context_path + "/api/v1/companies/" + companyId,
              contentType: "application/json;charset=utf-8",
              type: "DELETE",
              headers: {
                Accept: "application/json"
              },
              success: function(data, textStatus) {
                //console.log(data);
                swal("删除成功!", {
                  icon: "success",
                  timer: 2000,
                }).then(function(value) {
                  window.location.reload();
                });
              },
              error: function(data, textStatus) {
                //console.log(data.status);
                //console.log(textStatus);
                if(data.status == 400) {
                  swal({
                    title: "删除失败!",
                    text: "该企业已创建权益无法删除!",
                    icon: "error",
                    //timer: 2000,
                  });
                } else {
                  swal({
                    title: "删除失败!",
                    text: data.responseJSON.message,
                    icon: "error",
                  });
                }
              }
            });
          } else {
            //swal("删除失败!", {
              //timer: 2000,
            //});
          }
        });
      }

      function openResetPwdModal(id, phone) {
        $('#modal5 input[name="phone"]').val(phone);
        M.updateTextFields();
        $('#modal5 input[name="id"]').val(id);
        $('#modal5').modal('open');
      }

      function resetPwdSubmit() {
        var input_elems = $('#modal5 .modal-content input');
          input_elems.each(function(){
            M.validate_field($(this));
          });
          if($('#modal5 input[name="newPassword"]').val() != $('#modal5 input[name="confirmPassword"]').val() || $('#modal5 input[name="confirmPassword"]').val() == '') {
            $('#modal5 input[name="confirmPassword"]').addClass('invalid');
          } else {
            $('#modal5 input[name="confirmPassword"]').removeClass('invalid');
          }
          var invalid_input_elems = $('#modal5 .modal-content input.invalid');
          if(invalid_input_elems.length > 0) {
            return false;
          }
        $.ajax({
          url: context_path + "/api/v1/companies/" + $('#modal5 input[name="id"]').val() + "/admin/password?verificationCode=" + $('#modal5 input[name="code"]').val() + "&newPassword=" + encodeURI($('#modal5 input[name="newPassword"]').val()),
          data: '{"verificationCode" : "' + $('#modal5 input[name="code"]').val()
            + '", "newPassword" : "' + encodeURI($('#modal5 input[name="newPassword"]').val()) + '"}',
          contentType: "application/json;charset=utf-8",
          type: "POST",
          headers: {
            Accept: "application/json"
          },
          success: function(data, textStatus) {
            //console.log(data);
            swal({
              title: "",
              text: "保存成功!",
              icon: "success",
              timer: 2000,
            });
            //window.location.reload();
            $("#modal5 input[type=reset]").trigger("click");
            $('#modal5').modal('close');
          },
          error: function (data, textStatus, errorThrown) {
            //console.log(data);
            swal({
              title: "",
              text: data.responseJSON.message,
              icon: "error",
              timer: 2000,
            });
            M.updateTextFields();
          }
        });
      }
      
      var countDown = 60;
      function setTime() {
    	  if(countDown == 0) {
    		  $('#modal5 #sendSmsBtn').removeClass("disabled").html("获取验证码");
    		  countDown = 60;
    	  } else {
    		  $('#modal5 #sendSmsBtn').addClass("disabled").html(countDown + " s后重新获取");
	    	  countDown--;
	    	  setTimeout(function() {
	    		  setTime();
	    	  }, 1000);
    	  }
      }
      
      function sendSms() {
    	  $.ajax({
          url: context_path + "/api/v1/companies/" + $('#modal5 input[name="id"]').val() + "/admin/vc",
          type: "POST",
          success: function(data, textStatus) {
            //console.log(data);
            swal({
              title: "",
              text: "发送成功!",
              icon: "success",
              timer: 2000,
            });
            setTime();
          },
          error: function (data, textStatus, errorThrown) {
              // console.log(data);
            swal({
              title: "",
              text: "发送失败!",
              icon: "error",
              timer: 2000,
            });
          }
        });
      }

	  </script>
  </body>
</html>
